<template>
  <div class="abox">
    <div  class="haveData">
      <my-panel>
        <slot name="title">
          <div style="display: flex;flex-direction: row;align-items: center">
            <div class="little-dot1"></div>
            <div class="title">企业培训概况</div>
          </div>
        </slot>
        <div class="title-line"></div>
        <div class="num-data">
          <div class="count-data1">
            <div class="text-show">
              培训学时
            </div>
            <div class="num">
              {{totalData.trainTime}}
            </div>
          </div>
          <div class="count-data2">
            <div class="text-show">
              培训场次
            </div>
            <div class="num">
              {{totalData.trainSession}}
            </div>
          </div>
          <div class="count-data3">
            <div class="text-show">
              培训人次
            </div>
            <div class="num">
              {{totalData.persionTime}}
            </div>
          </div>
        </div>
      </my-panel>
      <my-panel>
        <slot name="title">
          <div style="display: flex;flex-direction: row;align-items: center">
            <div class="little-dot2"></div>
            <div class="title">企业培训趋势</div>
          </div>
        </slot>
        <div class="title-line"></div>
        <div class="date-choose">
          <div class="date-text">日期范围</div>
          <div class="solid-date six-month"  @click="getLastSixMon" :class="{'un-selected-node':timeType!=1,'selected-node':timeType==1}">近6个月</div>
          <div class="solid-date current-year" @click="getThisYear" :class="{'un-selected-node':timeType!=2,'selected-node':timeType==2}">今年</div>
          <div class="solid-date set-self" :class="{'un-selected-node':timeType!=3,'selected-node':timeType==3}" @click="chooseTimeFun">自定义</div>
        </div>
        <div class="show-time" @click="chooseTime">{{startTime.replace('-','/')}}--{{endTime.replace('-','/')}}</div>
        <div id="trend-chart" class="extend-line"></div>
        <div class="list-title">数据详情</div>
        <div style="width: 100%;height: auto;margin-bottom: 0.44rem">
          <div class="row first-row">
            <div class="cell first-cell">日期</div>
            <div class="cell">培训学时</div>
            <div class="cell">培训场次</div>
            <div class="cell">培训人数</div>
            <div class="cell">培训人次</div>
          </div>
          <div v-for="item in monthList" class="row other-row">
            <div class="cell first-cell">{{item.activityTime}}</div>
            <div class="cell">{{item.trainTime}}</div>
            <div class="cell">{{item.trainSession}}</div>
            <div class="cell">{{item.personCount}}</div>
            <div class="cell">{{item.persionTime}}</div>
          </div>
        </div>
      </my-panel>
    </div>
    <month-picker ref="countMonthPicker" v-if="showMonthPicker" v-model="showMonthPicker" @confirm="confirm" :init="[startTime,endTime]"></month-picker>
  </div>
</template>
<script>
  import {apiConstants} from '@/js/utils';
  import popup from 'ssit-mobile-popup';
  import MonthPicker from "../MonthPicker";
  var chartDom;
  var myChart;
  var option;
  export default {
    props: {
      companyId:{
        type:Number,
        default(){
          return -1;
        }
      }
    },
    data() {
      return {
        timeType:-1,//1近六个月 ,2今年 ,3自定义
        startTime:"",
        endTime:"",
        haveFlag: 1,
        showMonthPicker:false,
        totalData: {
          trainTime: 0,
          trainSession: 0,
          persionTime: 0,
        },
        monthList:[],
        arrMonth: [],
        arrTrainTime:[],
        arrTrainSession:[],
        arrPersionTime:[],
        typeList:["学时","场次","人次"]
      }
    },
    components: {popup,MonthPicker},
    created() {
    },
    mounted(){
      this.drawLine();
      window.onresize = function () {
        myChart.resize();
      }
    },
    methods: {
      confirm(val){
        let startMonth = parseInt(val[0].split("-")[0])*12+parseInt(val[0].split("-")[1]);
        let endMonth = parseInt(val[1].split("-")[0])*12+parseInt(val[1].split("-")[1]);
        if(endMonth<startMonth){
          this.$Message({
            content: `结束时间不能小于开始时间`,
            time: 2000,
            type: 'default',
            hasClose: true
          });
        }else if(endMonth-startMonth>12){
          this.$Message({
            content: `统计周期不能超过12个月`,
            time: 2000,
            type: 'default',
            hasClose: true
          });
        }else{
          this.startTime = val[0];
          this.endTime = val[1];
          this.loadData();
        }
      },
      loadData() {
        let _this = this;
        this.$ajax(apiConstants.company_bzxyStatistic, {
          data: {companyId: this.companyId,startTime:this.startTime,endTime:this.endTime},
          isJson:true,
          success: data => {
            _this.haveFlag = data.haveFlag;
            if(_this.haveFlag==1){
              _this.totalData = data.totalData;
              _this.monthList = data.perMonthData;
              _this.clearEchartArr();
              for(var i=0;i<_this.monthList.length;i++){
                _this.arrMonth.push(_this.monthList[i].activityTime.split("-")[1]+"月");
                _this.arrPersionTime.push(_this.monthList[i].persionTime)
                _this.arrTrainSession.push(_this.monthList[i].trainSession)
                _this.arrTrainTime.push(_this.monthList[i].trainTime)
              }
              _this.setOption();
            }
          },
          fail:data=>{

          }
        })
      },
      loadCompanyList(){
        let _this = this;
        this.$ajax(apiConstants.company_getAgentCompanyList, {
          data: {},
          success: function (data) {
            _this.companyList = data || [];
          }
        })
      },
      chooseTimeFun(){
        if(this.timeType==3){
          return
        }
        this.timeType = 3;
        this.setLatestXMonth(3);
        this.loadData();
      },
      chooseTime(){
        if(this.timeType == 3){
          this.showMonthPicker = true;
        }
      },
      drawLine(){
        chartDom = document.getElementById('trend-chart');
        myChart = echarts.init(chartDom);
      },
      setOption(){
        let fontSize = 0.24
        option = {};
        option = {
          tooltip: {
            trigger: 'axis',
            textStyle: {
              fontSize:fontSize*this.$baseSize
            }
          },
          legend: {
            data: this.typeList,
            textStyle: {
              fontSize: 0.26*this.$baseSize
            },
            selected: {
              '场次': false,
              '人次': false
            },
            itemGap:0.4*this.$baseSize
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            data: this.arrMonth,
            axisLine:{
              lineStyle:{
                color:'#ED0000;',
                width:2
              }
            },
            axisLabel: {
              textStyle: {
                fontSize: fontSize*this.$baseSize
              },
              //X轴刻度配置
              interval:0, //0：表示全部显示不间隔；auto:表示自动根据刻度个数和宽度自动设置间隔个数

              rotate: -80//倾斜度可以根据需求设
            },
          },
          yAxis: {
            type: 'value',
            axisLine:{
              show:true,
              lineStyle:{
                color:'#ED0000;',
                width:2
              }
            },
            axisLabel: {
              textStyle: {
                fontSize: fontSize*this.$baseSize
              },
            },
          },
          series: [
            {
              name:this.typeList[0],
              smooth: true,
              type: 'line',
              color:['#3C6EFF'],  //折线条的颜色
              symbolSize:8,
              lineStyle:{ //lineStyle里面写y轴那一条线的样式
                type: 'solid',
                width:3, //轴线的粗细 我写的是2 最小为0，值为0的时候线隐藏
              },
              data: this.arrTrainTime
            },
            {
              name:this.typeList[1],
              smooth: true,
              type: 'line',
              color:['#FF723C'],
              symbolSize:8,
              lineStyle:{ //lineStyle里面写y轴那一条线的样式
                type: 'solid',
                width:3, //轴线的粗细 我写的是2 最小为0，值为0的时候线隐藏
              },
              data: this.arrTrainSession
            },
            {
              name:this.typeList[2],
              smooth: true,
              type: 'line',
              symbolSize:8,
              color:[' #74D400'],
              lineStyle:{ //lineStyle里面写y轴那一条线的样式
                type: 'solid',
                width:3, //轴线的粗细 我写的是2 最小为0，值为0的时候线隐藏
              },
              data: this.arrPersionTime
            }]
        }
        option && myChart.setOption(option);
      },
      clearEchartArr(){
        this.arrMonth = [];
        this.arrTrainTime = [];
        this.arrTrainSession = [];
        this.arrPersionTime = [];
      },
      getThisYear(){
        this.timeType = 2;
        let data = new Date();
        let year = data.getFullYear();
        this.startTime = year+"-01";
        this.endTime = year+"-12";
        this.loadData();
      },
      getLastSixMon(reload) {
        if(this.timeType==1&&!reload){
          return;
        }
        this.timeType = 1;
        this.setLatestXMonth(6)
        this.loadData();
      },
      setLatestXMonth(x){
        let data = new Date();
        //获取年
        let year = data.getFullYear();
        //获取月
        let mon = data.getMonth() + 1;
        for (let i = 0; i < x; i++) {
          if (mon <= 0) {
            year = year - 1;
            mon = mon + 12;
          }
          if (mon < 10) {
            mon = "0" + mon;
          }
          if (i == 0) {
            this.endTime = year + "-" + mon
          }
          if(i==x-1){
            this.startTime = year + "-" + mon
          }
          mon = mon - 1;
        }
      }
    },
    watch:{
      companyId(){
        if(this.companyId>0){
          this.getLastSixMon(true);
        }
      }
    }
  }

</script>
<style lang="scss" scoped>
  .num {
    width:1.16rem;
    height: 0.36rem;
    font-size: 0.38rem;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #333333;
    line-height: 0.36rem;
    letter-spacing: 0.01rem;
    margin-top: 0.2rem;
    text-align: center;
  }
</style>
<style lang="scss">
  .abox{
    width: 100%;
    height: 100%;
    overflow-y: auto;
    flex-grow: 1;
    display: flex;
  }

  .haveData {
    width: 100%;
    display: flex;
    margin-top: 0.1rem;
    flex-direction: column;
    flex-grow: 1;
    overflow: auto;
    padding-bottom: 0.1rem;
  }
  .little-dot1{
    width: 0.12rem;
    height: 0.12rem;
    background: #FFF265;
    border-radius: 0.10rem;
  }
  .little-dot2{
    width: 0.12rem;
    height: 0.12rem;
    background: #FFA965;
    border-radius: 0.06rem;
  }
  .title{
    height: 0.32rem;
    font-size: 0.32rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    line-height: 0.32rem;
    letter-spacing: 0.01rem;
    margin-left: 0.1rem;
  }
  .title-line{
    height: 0.01rem;
    background: #F4F6F8;
    margin-top: 0.27rem;
  }
  .num-data {
    margin-top: 0.38rem;
    height: auto;
    display: inline-flex;
    flex-direction: row;
    justify-content: space-between;
    margin-bottom: 0.61rem;
  }
  .count-data1 {
    display: flex;
    margin-left: 0.39rem;
    flex-direction: column;
    align-items: center;
  }

  .count-data2 {
    display: flex;
    flex-direction: column;
    align-items: center;
  }
  .count-data3 {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-right: 0.39rem;
  }
  .text-show {
    width: 1.16rem;
    height: 0.28rem;
    font-size: 0.24rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #999999;
    line-height: 0.28rem;
    text-align: center;
  }

  .date-choose{
    height:0.54rem;
    width: 100%;
    margin-top: 0.4rem;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
  }
  .date-text{
    margin-right: .4rem;
    height: 0.28rem;
    font-size: 0.24rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;
    line-height: 0.28rem;
    flex-shrink: 0;

  }
  .solid-date{
    width: 1.50rem;
    height: 0.54rem;
    margin-right: .2rem;
    border-radius: 0.35rem;
    font-size: 0.24rem;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    line-height: 0.54rem;
    text-align: center;
    flex-shrink: 0;
    @media only screen and  (max-device-width:370px){
      flex-shrink: 1;
    }
  }
  .six-month{
    color: #999999;
    background: #F5F6F8;
  }
  .current-year{
  }
  .set-self{
  }
  .selected-node{
    color: #005EED;
    background: #FFFFFF;
    border: 0.01rem solid #005EED;
  }
  .un-selected-node{
    color: #999999;
    background: #F5F6F8;
  }
.show-time{
  height: 0.68rem;
  background: #F5F6F8;
  border-radius: 0.14rem;
  margin-top: 0.4rem;
  line-height: 0.68rem;
  text-align: center;
  font-size: 0.28rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
  letter-spacing: 0.02rem;
}
.extend-line{
  width: 100%;
  height: 4.09rem;
  margin-top: 0.5rem;
}
.list-title{
  margin-top: 0.44rem;
  height: 0.28rem;
  font-size: 0.24rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #333333;
  line-height: 0.28rem;
  text-align: left;
  margin-bottom: 0.3rem;
}
.no-data-row{
  text-align: center;
  border:0.01rem solid #D8D8D8;
  border-top-width: 0;
  line-height: .73rem;
}
.other-row{
  color: #333333;
}
.row{
  width: 100%;
  min-height:0.73rem;
  max-height: 1.4rem;
  text-align: center;
  font-size: 0.24rem;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  line-height: 0.73rem;
  display: flex;
  flex-direction: row;
  .cell{
    display: flex;
    flex:1;
    align-items: center;
    justify-content: center;
    border-right: 0.01rem solid #D8D8D8;
    border-bottom: 0.01rem solid #D8D8D8;
  }
  .first-cell{
    border-left:0.01rem solid #D8D8D8;
  }
}
.first-row{
  color: #999999;
  .cell{
    border-top:0.01rem solid #D8D8D8
  }
}

  .noData {
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
    flex-direction: column;
    .icon {
      margin-top: 1.61rem;
      width: 1.2rem;
      height: 1.2rem;
      background: url(/images/team/wushuju01.png) no-repeat center;
    }
    .text {
      margin-top: 0.49rem;
      font-family: "PingFang SC-Regular";
      text-align: center;
      color: #999999;
      font-size: 0.28rem;
    }
    .create-button {
      height: 0.9rem;
      width: 5.46rem;
      margin-top: 1.14rem;
      font-family: "PingFang SC-Regular";
      text-align: center;
      color: #ffffff;
      font-size: 0.28rem;
      background-color: #4985f5;
      line-height: 0.9rem;
      border-radius: 0.6rem
    }
  }

</style>
